package nero.yncc.com.createart.activity;

import android.content.Intent;
import android.content.IntentFilter;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.Timer;
import java.util.TimerTask;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.base.BaseActivity;
import nero.yncc.com.createart.receiver.BatteryReceiver;

/**
 * Created by nero on 17-6-20.
 */

@ContentView(R.layout.activity_login)
public class ActicityLogin extends BaseActivity {
    @ViewInject(R.id.btn_login)
    TextView btn_login;

    @ViewInject(R.id.iv_login_pics)
    ImageView iv_login_pics;

    @ViewInject(R.id.tv_register)
    TextView tv_register;

    int[] picIDs = {R.mipmap.login1, R.mipmap.login2, R.mipmap.login3};  //登录页了轮流跟换的图片id

    @Override
    protected void initView() {
        super.initView();
//        byScroll(true);
        setStateBarColor(R.color.statebar_gray);
        startAnimation(1);

//
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        //创建广播接受者对象
        BatteryReceiver batteryReceiver = new BatteryReceiver();

        //注册receiver
        registerReceiver(batteryReceiver, intentFilter);
    }

    @Override
    protected void initListener() {
        super.initListener();
        btn_login.setOnClickListener(this);
        tv_register.setOnClickListener(this);
    }

    @Override
    protected void doClick(int id) {
        super.doClick(id);
        switch (id) {
            case R.id.btn_login:
                Intent intent = new Intent(mContext, ActivityAccountOrPhoneLogin.class);
                startActivity(intent);
                break;
            case R.id.tv_register:
                Intent intent2 = new Intent(mContext, ActivityRegister.class);
                startActivity(intent2);
                break;
        }
    }

    boolean isChange = false;

    private void startAnimation(final int num) {

        AlphaAnimation alphaAnimationStart = new AlphaAnimation(1, 0);
        alphaAnimationStart.setDuration(2000);
        alphaAnimationStart.setFillAfter(true);
        iv_login_pics.startAnimation(alphaAnimationStart);
        alphaAnimationStart.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                iv_login_pics.setImageResource(picIDs[num]);

                AlphaAnimation alphaAnimationEnd = new AlphaAnimation(0f, 1.0f);
                alphaAnimationEnd.setDuration(1520);
                alphaAnimationEnd.setFillAfter(true);
                iv_login_pics.startAnimation(alphaAnimationEnd);
                alphaAnimationEnd.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        new Timer().schedule(new TimerTask() {
                            @Override
                            public void run() {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (num == picIDs.length - 1) {
                                            startAnimation(0);
                                        } else {
                                            startAnimation(num + 1);
                                        }
                                    }
                                });
                            }
                        },1000);


                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });


    }


}
